package code.D1222;

/**
 * author： yeswater
 * create： 2023/12/22
 *
 * 二叉树的直径：任意两个节点之间最长路径的长度
 *
 */
public class T543 {
    int ans = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        dfs(root);
        return ans;
    }
    
    private int dfs(TreeNode node) {
        if (node ==null) {
            return 0;
        }
        int l = dfs(node.left), r = dfs(node.right);
        ans = Math.max(ans, l + r);
        return Math.max(l, r) + 1;
    }
    
    private class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        
        TreeNode() {
        }
        
        TreeNode(int val) {
            this.val = val;
        }
        
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
